/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.hmvc4gwt.hmvc.gwt.client.module;

import java.util.Map;

import com.googlecode.hmvc4gwt.hmvc.core.component.Component;
import com.googlecode.hmvc4gwt.hmvc.core.event.IEvent;
import com.googlecode.hmvc4gwt.hmvc.core.event.IEventListener;
import com.googlecode.hmvc4gwt.hmvc.gwt.client.controller.AControllerGwt;
import com.googlecode.hmvc4gwt.hmvc.gwt.client.events.in.AddViewEvent;
import com.googlecode.hmvc4gwt.hmvc.gwt.client.events.in.RemoveViewEvent;
import com.googlecode.hmvc4gwt.hmvc.gwt.client.rootpanel.RootPanelController;
import com.googlecode.hmvc4gwt.hmvc.gwt.client.view.IViewGwt;

/**
 * This is the basic IModuleController implementation. Extend this class to
 * create your EntryPoints.
 * 
 * This implementation adds and removes all Widgets to the RootPanel.
 * 
 * @author jens.meiss
 */
public abstract class AModuleController extends AControllerGwt implements
		IModuleController {

	private RootPanelController rootPanelController;

	/**
	 * @param view
	 * @see com.googlecode.hmvc4gwt.hmvc.gwt.client.rootpanel.RootPanelController#addView(com.googlecode.hmvc4gwt.hmvc.gwt.client.view.IViewGwt)
	 */
	public void addView(IViewGwt view) {
		rootPanelController.addView(view);
	}

	/**
	 * @param view
	 * @see com.googlecode.hmvc4gwt.hmvc.gwt.client.rootpanel.RootPanelController#removeView(com.googlecode.hmvc4gwt.hmvc.gwt.client.view.IViewGwt)
	 */
	public void removeView(IViewGwt view) {
		rootPanelController.removeView(view);
	}

	/**
	 * @param rootPanelController the rootPanelController to set
	 */
	private void setRootPanelController(RootPanelController rootPanelController) {
		this.rootPanelController = rootPanelController;
	}

	/**
	 * Override this method only if you know wath you'r doing ;)
	 * 
	 * An example how to override is the AModuleControllerRegistry class
	 */
	public void onModuleLoad() {
		new Component(this, null, null);
	}
	
	public final void handleEvent(IEvent event) {

		if (event instanceof AddViewEvent) {
			addView(((AddViewEvent) event).getView());
			return;
		} else if (event instanceof RemoveViewEvent) {
			removeView(((RemoveViewEvent) event).getView());
			return;
		}
		
		/*
		 * delegating event to the implementing class
		 */
		handleEventImpl(event);
	}

	public final void init() {
		
		setRootPanelController(new RootPanelController());
		
		initImpl();
	}

	public void terminate() {
		terminateImpl();
	}
	
	public void registerEventListener(
			Map<Class<? extends IEvent>, IEventListener> mapEventListener) {
		
		mapEventListener.put(AddViewEvent.class, this);
		mapEventListener.put(RemoveViewEvent.class, this);
		
		registerEventListenerImpl(mapEventListener);
	}
}
